﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace DAH_TD
{
    public class Tower
    {
        int type;

        public Texture2D texture;

        TimeSpan attackSpeedTimeSpan;
        TimeSpan previousAttack;

        public Rectangle boundarie;
        Rectangle rangeRectangle;

        float damage;
            public float getDamage() { return this.attackSpeed; }
        float attackSpeed;
            public float getAttackSpeed() { return this.attackSpeed; }
        float range;
            public float getRange() { return this.range; }
        float costs;
            public float getCosts() { return this.costs; }
        TileMap map;
        Vector2 position;

         public Vector2 getPosition() { return this.position; }

        public Tower(TileMap map, int type, Texture2D texture, Vector2 position)
        {
            this.map = map;
            this.texture = texture;
            this.position = position;
            this.boundarie = new Rectangle((int)position.X, (int)position.Y, 50, 50);
            this.boundarie.X = (int)getPos().X * 50;
            this.boundarie.Y = (int)getPos().Y * 50;

            
            switch (type)
            {
                case 0:
                    type = 0;
                    break;
                case 1:
                    {
                        this.type = 1;
                        this.damage = 10f;
                        this.attackSpeed = 1f;
                        this.range = 1f;
                        break;
                    }
            }
            
            this.attackSpeedTimeSpan = TimeSpan.FromSeconds((double)this.attackSpeed);
            this.rangeRectangle = new Rectangle(this.boundarie.X - ((int)this.range * this.map.getTileWidth()), this.boundarie.Y - ((int)this.range * this.map.getTileHeight()), (2 * (int)this.range * this.map.getTileWidth()) + this.map.getTileWidth(), (2 * (int)this.range * this.map.getTileHeight()) + this.map.getTileHeight());
        }

        public Vector2 getPos()
        {
            Vector2 actualTile;
            int b = map.getTileWidth();
            float xTile = position.X / b;
            int xPos = (int)xTile;
            actualTile.X = xPos;
            // in y-Richtung: 
            int h = map.getTileHeight();
            float yTile = position.Y / h;
            int yPos = (int)yTile;
            actualTile.Y = yPos;

            return actualTile;
        }

        public bool checkIfAttack(GameTime gt) {
            if (gt.TotalGameTime - previousAttack > attackSpeedTimeSpan)
            {
                previousAttack = gt.TotalGameTime;
                return true;
            }
            else { return false; }
        }

        public void attackEnemy(Enemy enemyToAttack) {
            enemyToAttack.setHp(enemyToAttack.getHp() - this.damage);
        }

        public List<Enemy> enemiesInTowerRange(List<Enemy> enemies)
        {
            List<Enemy> enemiesInRange = new List<Enemy>();
            // für jeden Turm wird die Position bestimmt und anhand der Reichweite (in Kacheln) mögliche Gegener ermittelt
            for (int i = 0; i < enemies.Count; i++)
            {
                if (this.rangeRectangle.Intersects(enemies[i].getBoundarie()))
                {
                    enemiesInRange.Add(enemies[i]);
                }
            }
            return enemiesInRange;
        }

        public bool ifEnemiesInTowerRange(List<Enemy> enemies)
        {
            List<Enemy> enemiesInRange = new List<Enemy>();
            for (int i = 0; i < enemies.Count; i++ )
            {
                if (this.rangeRectangle.Intersects(enemies[i].getBoundarie())){
                    enemiesInRange.Add(enemies[i]);
                }
            }
            
            
            if (enemiesInRange.Count >= 1) {return true;}
            else {return false;}
        }

    }
}
